use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // Create sys_params table
        manager
            .create_table(
                Table::create()
                    .table(SysParams::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysParams::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysParams::ParamName).string().string_len(100))
                    .col(ColumnDef::new(SysParams::ParamType).integer())
                    .col(ColumnDef::new(SysParams::ParamKey).string().string_len(100))
                    .col(ColumnDef::new(SysParams::ParamValue).string().string_len(100))
                    .col(ColumnDef::new(SysParams::Remark).string().string_len(100))
                    .col(ColumnDef::new(SysParams::TenantId).big_integer())
                    .col(ColumnDef::new(SysParams::Creator).big_integer())
                    .col(ColumnDef::new(SysParams::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(SysParams::Updater).big_integer())
                    .col(ColumnDef::new(SysParams::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysParams::Version).integer().default(0))
                    .col(ColumnDef::new(SysParams::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        let now = Expr::cust("CURRENT_TIMESTAMP"); // 使用数据库的 now()

        manager
            .exec_stmt(
                Query::insert()
                    .into_table(SysParams::Table)
                    .columns(vec![
                        SysParams::ParamName,
                        SysParams::ParamType,
                        SysParams::ParamKey,
                        SysParams::ParamValue,
                        SysParams::Remark,
                        SysParams::TenantId,
                        SysParams::Version,
                        SysParams::Deleted,
                        SysParams::Creator,
                        SysParams::CreatedAt,
                        SysParams::Updater,
                        SysParams::UpdatedAt,
                    ])
                    .values(vec![
                        "用户登录-验证码开关".into(),
                        1.into(),
                        "LOGIN_CAPTCHA".into(),
                        "false".into(),
                        "是否开启验证码（true：开启，false：关闭）".into(),
                        10000.into(),
                        0.into(),
                        0.into(),
                        10000.into(),
                        now.clone().into(), // create_time
                        10000.into(),
                        now.into(),        // update_time
                    ]).unwrap()
                    .to_owned(),
            )
            .await?;


        // Create sys_attachment table
        manager
            .create_table(
                Table::create()
                    .table(SysAttachment::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysAttachment::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysAttachment::Name).string())
                    .col(ColumnDef::new(SysAttachment::Url).string())
                    .col(ColumnDef::new(SysAttachment::Size).big_integer())
                    .col(ColumnDef::new(SysAttachment::Platform).string())
                    .col(ColumnDef::new(SysAttachment::TenantId).big_integer())
                    .col(ColumnDef::new(SysAttachment::ContentType).string())
                    .col(ColumnDef::new(SysAttachment::Creator).big_integer())
                    .col(ColumnDef::new(SysAttachment::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(SysAttachment::Updater).big_integer())
                    .col(ColumnDef::new(SysAttachment::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysAttachment::Version).integer().default(0))
                    .col(ColumnDef::new(SysAttachment::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SysAttachment::Table).to_owned())
            .await?;
        
        manager
            .drop_table(Table::drop().table(SysParams::Table).to_owned())
            .await?;

        Ok(())
    }
}

#[derive(DeriveIden)]
enum SysParams {
    Table,
    Id,
    ParamName,
    ParamType,
    ParamKey,
    ParamValue,
    Remark,
    TenantId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}

#[derive(DeriveIden)]
enum SysAttachment {
    Table,
    Id,
    Name,
    Url,
    Size,
    Platform,
    TenantId,
    ContentType,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}